// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

import { CupertinoFontSettings, CupertinoPalette } from "styling.slint";
import { ScrollBar } from "scrollview.slint";
import { FocusBorder } from "components.slint";

// FIXME: After auto-hiding of scrollbars is implemented, use TextEditBase
component ScrollView {
    in property <bool> enabled: true;
    out property <length> visible-width <=> flickable.width;
    out property <length> visible-height <=> flickable.height;
    in-out property <length> viewport-width <=> flickable.viewport-width;
    in-out property <length> viewport-height <=> flickable.viewport-height;
    in-out property <length> viewport-x <=> flickable.viewport-x;
    in-out property <length> viewport-y <=> flickable.viewport-y;
    // FIXME: remove. This property is currently set by the ListView and is used by the native style to draw the scrollbar differently when it has focus
    in-out property <bool> has-focus;

    min-height: 50px;
    min-width: 50px;
    horizontal-stretch: 1;
    vertical-stretch: 1;
    preferred-height: 100%;
    preferred-width: 100%;

    flickable := Flickable {
        x: 2px;
        y: 2px;
        interactive: false;
        viewport-y <=> vertical-bar.value;
        viewport-x <=> horizontal-bar.value;
        width: parent.width - 16px;
        height: 100%;

        @children
    }

    vertical-bar := ScrollBar {
        enabled: root.enabled;
        x: parent.width - self.width;
        y: 0;
        width: self.has-hover ? 20px : 12px;
        height: horizontal-bar.visible ? parent.height - horizontal-bar.height : parent.height;
        horizontal: false;
        maximum: flickable.viewport-height - flickable.height;
        page-size: flickable.height;
        visible: flickable.viewport-height > flickable.height;
    }

    horizontal-bar := ScrollBar {
        enabled: root.enabled;
        width: vertical-bar.visible ? parent.width - vertical-bar.width : parent.width;
        height: self.has-hover ? 20px : 12px;
        y: parent.height - self.height;
        x: 0;
        horizontal: true;
        maximum: flickable.viewport-width - flickable.width;
        page-size: flickable.width;
        visible: flickable.viewport-width > flickable.width;
    }
}

export component TextEdit {
    in property <TextWrap> wrap <=> text-input.wrap;
    in property <TextHorizontalAlignment> horizontal-alignment <=> text-input.horizontal-alignment;
    in property <bool> read-only <=> text-input.read-only;
    in property <length> font-size <=> text-input.font-size;
    in property <bool> enabled <=> text-input.enabled;
    out property <length> visible-width <=> scroll-view.visible-width;
    out property <length> visible-height <=> scroll-view.visible-height;
    in-out property <bool> has-focus: text-input.has-focus;
    in-out property <string> text <=> text-input.text;
    in-out property <length> viewport-x <=> scroll-view.viewport-x;
    in-out property <length> viewport-y <=> scroll-view.viewport-y;
    in-out property <length> viewport-width <=> scroll-view.viewport-width;
    in-out property <length> viewport-height <=> scroll-view.viewport-height;
    in property <string> placeholder-text;

    callback edited(text: string);
    callback key-pressed(event: KeyEvent) -> EventResult;
    callback key-released(event: KeyEvent) -> EventResult;

    accessible-role: AccessibleRole.text-input;
    accessible-enabled: root.enabled;
    accessible-value <=> text;
    accessible-placeholder-text: text == "" ? placeholder-text : "";
    accessible-read-only: root.read-only;

    public function set-selection-offsets(start: int, end: int) {
        text-input.set-selection-offsets(start, end);
    }

    public function select-all() {
        text-input.select-all();
    }

    public function clear-selection() {
        text-input.clear-selection();
    }

    public function cut() {
        text-input.cut();
    }

    public function copy() {
        text-input.copy();
    }

    public function paste() {
        text-input.paste();
    }

    forward-focus: text-input;
    horizontal-stretch: 1;
    vertical-stretch: 1;

    states [
        disabled when !root.enabled: {
            text-input.color: CupertinoPalette.foreground-secondary;
            background.background: CupertinoPalette.tertiary-control-background;
        }
        focused when root.has-focus: {
            background.background: CupertinoPalette.control-background;
        }
    ]

    FocusBorder {
        x: (parent.width - self.width) / 2;
        y: (parent.height - self.height) / 2;
        width: parent.width + 6px;
        height: parent.height + 6px;
        has-focus: root.has-focus;
    }

    background := Rectangle {
        background: CupertinoPalette.alternate-background;
        border-color: CupertinoPalette.border;
        border-width: 1px;
    }

    ContextMenuArea {
        Menu {
            MenuItem {
                title: @tr("Cut");
                activated => { text-input.cut(); }
            }
            MenuItem {
                title: @tr("Copy");
                activated => { text-input.copy(); }
            }
            MenuItem {
                title: @tr("Paste");
                activated => { text-input.paste(); }
            }
            MenuItem {
                title: @tr("Select All");
                activated => { text-input.select-all(); }
            }
        }

        scroll-view := ScrollView {
            x: 8px;
            y: 8px;
            width: parent.width - 16px;
            height: parent.height - 16px;
            viewport-width: root.wrap == TextWrap.no-wrap ? max(self.visible-width, text-input.preferred-width) : self.visible-width;
            viewport-height: max(self.visible-height, text-input.preferred-height);

            text-input := TextInput {
                enabled: true;
                color: CupertinoPalette.foreground;
                font-size: CupertinoFontSettings.body.font-size;
                font-weight: CupertinoFontSettings.body.font-weight;
                selection-background-color: CupertinoPalette.selection-background;
                selection-foreground-color: self.color;
                single-line: false;
                wrap: word-wrap;
                page-height: scroll-view.visible-height;
                // Disable TextInput's built-in accessibility support as the widget takes care of that.
                accessible-role: none;

                edited => {
                    root.edited(self.text);
                }

                key-pressed(event) => {
                    root.key-pressed(event)
                }

                key-released(event) => {
                    root.key-released(event)
                }

                cursor-position-changed(cpos) => {
                    if (cpos.x + root.viewport-x < 12px) {
                        root.viewport-x = min(0px, max(parent.visible-width - self.width,  - cpos.x + 12px));
                    } else if (cpos.x + root.viewport-x > parent.visible-width - 12px) {
                        root.viewport-x = min(0px, max(parent.visible-width - self.width,  parent.visible-width - cpos.x - 12px));
                    }
                    if (cpos.y + root.viewport-y < 12px) {
                        root.viewport-y = min(0px, max(parent.visible-height - self.height,  - cpos.y + 12px));
                    } else if (cpos.y + root.viewport-y > parent.visible-height - 12px - 20px) {
                        // FIXME: font-height hardcoded to 20px
                        root.viewport-y = min(0px, max(parent.visible-height - self.height,  parent.visible-height - cpos.y - 12px - 20px));
                    }
                }
            }
        }
    }

    placeholder := Text {
        x: scroll-view.x;
        y: scroll-view.y;
        width: scroll-view.width;
        vertical-alignment: top;
        text: (root.text == "" && text-input.preedit-text == "") ? root.placeholder-text : "";
        font-size: text-input.font-size;
        font-italic: text-input.font-italic;
        font-weight: text-input.font-weight;
        font-family: text-input.font-family;
        color: CupertinoPalette.foreground-secondary;
        overflow: elide;
        // `accessible-placeholder-text` is set on TextEdit already
        accessible-role: none;
    }
}
